package game.birds 
{
	import com.greensock.*;
	import com.greensock.easing.*;
	import flash.display.*;
	import flash.events.*;
	import game.*;
	import windowsfla.*;
	/**
	 * ...
	 * @author Goretskiy Alex
	 */
	public class Bird extends BirdMC
	{
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		//  Protected variables
		//----------------------------------
		
		protected var _type:int;
		protected var _direction:int;
		
		//--------------------------------------------------------------------------
		//
		//  Getters and setters
		//
		//--------------------------------------------------------------------------
		
		public function get type():int { return _type; }
		public function get direction():int { return _direction; }
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		//  Constructor
		//----------------------------------
		
		public function Bird() 
		{
			
		}
		
		//----------------------------------
		//  Protected methods
		//----------------------------------
		
		protected function killBird( e:MouseEvent ):void 
		{
			if ( Game.instance.ammunition > 0 )
			{
				if ( Game.instance.sight.hitTestObject( this ) )
				{
					Game.instance.scores += ( Settings.SCORES_PER_BIRD[type] * ( 2 - scaleX ) );
					
					const game_window:MovieClip = Utils.getChildByNameDeep( Game.instance.window, "game_window" ) as MovieClip;
					var kill:KillAnimationMC = new KillAnimationMC();
					kill.x = x;
					kill.y = y;
					game_window.addChild( kill );
					
					die();
				}
			}
		}
		
		//----------------------------------
		//  Public methods
		//----------------------------------
		
		/**
		 * 
		 * @param	e
		 */
		public function init( bird_type:int ):void 
		{
			_type = bird_type;
			Utils.PlayDeep( this, "bird_" + String( _type ) );
			
			addEventListener( MouseEvent.CLICK, killBird );
			
			const scale:Number = Utils.randNumber( 0, 3 ) / 10;
			
			scaleX = 1 - scale;
			scaleY = 1 - scale;
		}
		
		/**
		 * 
		 */
		public function update():void
		{
			for each ( var bird:* in Game.instance.birds )
			{
				if ( bird == this ) continue;
				//если направление у нас одинаковое - не сталкиваемся
				if ( direction == bird.direction ) continue;
				//смотрим на дальность расположения птиц
				if ( scaleX != bird.scaleX ) continue;
				
				if ( this.hitTestObject( bird ) )
				{
					const game_window:MovieClip = Utils.getChildByNameDeep( Game.instance.window, "game_window" ) as MovieClip;
					
					var boom:BoomAnimationMC = new BoomAnimationMC();
					boom.x = ( bird.x + x ) * .5 - width;
					boom.y = y;
					game_window.addChild( boom );
					
					//Убиваем птиц
					bird.die();
					die();
				}
			}
		}
		
		/**
		 * 
		 */
		public function destroy():void 
		{
			if ( parent )
			{
				parent.removeChild( this );
			}
			
			Game.instance.birds[this] = null;
			delete Game.instance.birds[this];
		}
		/**
		 * 
		 */
		protected function animationIsComplete():void 
		{
			if ( parent )
			{
				parent.removeChild( this );
			}
		}
		
		/**
		 * 
		 */
		public function die():void 
		{
			Game.instance.birds[this] = null;
			delete Game.instance.birds[this];
			
			TweenMax.to( this , 3, 
					{
						bezierThrough:[ 
							{ x: x, y: Settings.WORLD_HEIGHT + 100 } 
						],
						onComplete: animationIsComplete, 
						orientToBezier:true,
						ease:Bounce.easeOut
					}
				);
		}
		
	}

}